import { DialogForm, DialogTable } from 'custom/index'
import { getShopDistributeRuleList, distributeCusSer, distributeCompany, modifyCusSer, getModifyRecordList, getDistributeDateInfo, getCusSerListList, addTemporaryCustomer } from './api'
import { inputMethodOptions } from './constant'
import HalfInput from './components/HalfInput.vue'
import ExNameItem from './components/ExNameItem.vue'
import UserIdItem from './components/UserIdItem.vue'

const addAccountDialog = new DialogForm('DialogForm')
addAccountDialog
  .title('添加子账号')
  .useGroup('')
    .useItem('accountNo').label('数据账号').required().end()
    .useItem('accountName').label('子账号名称').required().end()
    .useItem('accountPassword').label('子账号密码').required().end()
    .useSelect('inputType', inputMethodOptions).label('录入方式').required().end()

const dispatchAccountDialog = new DialogForm('DialogForm')
dispatchAccountDialog
  .title('分配客服')
  .getApi(async (_, row) => {
    let userId = row.userId
    if (row.companyNo) {
      const userList = await getCusSerListList(row.companyNo, row.shopId)
      if (!userList.some(i => i.value === row.userId)) {
        userId = ''
      }
    } else {
      userId = ''
    }
    const { subAccountId, shopName, accountName, companyName, allocationRuleId, companyNo, startDate, endDate, baseSalary, conventionSalary, commissionRatio, shopId } = row
    return { subAccountId, shopName, accountName, companyName, allocationRuleId, companyNo, userId, startDate, endDate: endDate || '2188-01-01', baseSalary, conventionSalary, commissionRatio, shopId }
  })
  .postApi(distributeCusSer)
  .useGroup()
    .useItem('shopName').label('所属店铺').setProp('disabled', true).end()
    .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
    // .useItem('allocationRuleId').label('分配规则').required().end()
    .useApiSelect('allocationRuleId', getShopDistributeRuleList).label('分配规则').disabled(() => true).required().end()
    // .useDictSelect('exName', 'exName').label('分部公司').setProp('widthLabel', 'exNameLabel').required().end()
    // .useCustomItem(ExNameItem, 'companyNo').label('分部公司').required().end()
    // .useApiSelect('userId', getCusSerListList).label('选择客服').required().end()
    .useItem('companyName').label('分部公司').required().disabled(() => true).end()
    .useCustomItem(UserIdItem, 'userId').label('选择客服').required().end()
    .useDatePicker('startDate').label('开始日期').required().end()
    .useDatePicker('endDate').label('截止日期').end()
    .useItem('baseSalary').label('总底薪').setProp('disabled', true).end()
    // .useItem('conventionSalary').label('约定分摊底薪').required().end()
    .useCustomItem(HalfInput, 'conventionSalary').label('约定分摊底薪(元)').required().end()
    .useItem('commissionRatio').label('提成比例').disabled(() => true).end()
    // .useSelect('sendSmsSign', sendSmsSignOptions).label('短信提醒').required().end()

const dispatchAccountAllDialog = new DialogForm('DialogForm')
dispatchAccountAllDialog
.title('分配客服')
  .getApi(async (_, row) => {
    let userId = row.userId
    if (row.companyNo) {
      const userList = await getCusSerListList(row.companyNo, row.shopId)
      if (!userList.some(i => i.value === row.userId)) {
        userId = ''
      }
    } else {
      userId = ''
    }
    const { subAccountId, shopName, accountName, allocationRuleId, companyNo, baseSalary, conventionSalary, commissionRatio, shopId, commission } = row

    const dateInfo = await getDistributeDateInfo({ accountNo: row.accountNo })
    return { subAccountId, shopName, accountName, allocationRuleId, companyNo, userId, startDate: dateInfo.startDate, endDate: dateInfo.endDate || '2188-01-01', baseSalary, conventionSalary: conventionSalary || baseSalary, commissionRatio: commissionRatio === null ? commission : commissionRatio, shopId }
  })
  .postApi(distributeCompany)
  .useGroup()
    .useItem('shopName').label('所属店铺').setProp('disabled', true).end()
    .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
    .useApiSelect('allocationRuleId', getShopDistributeRuleList).label('分配规则').required().end()
    // .useItem('companyName').label('分部公司').required().end()
    .useCustomItem(ExNameItem, 'companyNo').label('分部公司').required().end()
    .useCustomItem(UserIdItem, 'userId').label('选择客服').end()
    .useDatePicker('startDate').label('开始日期').required().end()
    .useDatePicker('endDate').label('截止日期').end()
    .useItem('baseSalary').label('总底薪').setProp('disabled', true).end()
    .useCustomItem(HalfInput, 'conventionSalary').label('约定分摊底薪(元)').required().end()
    .useItem('commissionRatio').label('提成比例').end()

const dispatchCompanyDialog = new DialogForm('DialogForm')
dispatchCompanyDialog
  .title('分配公司')
  .getApi(async(_, row) => ({ subAccountId: row.subAccountId, companyNo: row.companyNo, companyName: row.companyName, companyBaseSalary: row.companyBaseSalary }))
  .postApi(distributeCompany)
  .useGroup()
    .useDictSelect('companyNo', 'company').setProp('widthLabel', 'companyName').label('公司').required().end()
    .useItem('companyBaseSalary').label('总公司分配底薪').required().end()

const dispatchEditDialog = new DialogForm('DialogForm')
dispatchEditDialog
  .title('修改')
  .getApi(async (_, row) => ({ companyNo: row.companyNo, userId: row.companyNo ? row.userId : '', subAccountId: row.subAccountId, accountName: row.accountName, startDate: row.startDate, endDate: row.endDate, conventionSalary: row.companyBaseSalary, commissionRatio: row.commissionRatio }))
  .postApi(modifyCusSer)
  .useGroup()
    .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
    .useCustomItem(ExNameItem, 'companyNo').label('分部公司').disabled(() => true).end()
    .useCustomItem(UserIdItem, 'userId').label('选择客服').disabled(props => !props.data.companyNo).end()
    .useDatePicker('startDate').label('开始日期').required().end()
    .useDatePicker('endDate').label('结束日期').required().end()
    .useCustomItem(HalfInput, 'conventionSalary').label('约定薪资').required().end()
    .useItem('commissionRatio').label('提成比例').end()

const dispatchEditWithCompanyDialog = new DialogForm('DialogForm')
  dispatchEditWithCompanyDialog
    .getApi(async (_, row) => ({ companyNo: row.companyNo, userId: row.userId, subAccountId: row.subAccountId, accountName: row.accountName, startDate: row.startDate, endDate: row.endDate, conventionSalary: row.conventionSalary }))
    .postApi(modifyCusSer)
    .useGroup()
      .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
      .useCustomItem(ExNameItem, 'companyNo').label('分部公司').disabled(() => true).end()
      .useCustomItem(UserIdItem, 'userId').label('选择客服').end()
      .useDatePicker('startDate').label('开始日期').required().end()
      .useDatePicker('endDate').label('结束日期').required().end()
      .useCustomItem(HalfInput, 'conventionSalary').label('约定薪资').required().end()
      .useItem('commissionRatio').label('提成比例').end()

const dispatchLogDialog = new DialogTable('DialogTable')
dispatchLogDialog
.title('修改记录')
.width('50%')
.api(async(_, row) => {
  return getModifyRecordList({ subAccountId: row.subAccountId })
})
.useTable()
  .addIndexColumn()
  .useColumn('modifyContent').label('修改项目').end()
  .useColumn('modifyBefore').label('修改前').end()
  .useColumn('modifyAfter').label('修改后').end()
  .useColumn('createTime').label('修改日期').end()
  .useColumn('createUserName').label('修改人').end()
  .upto('DialogTable')
.usePagination()

const temporaryHandlerDialog = new DialogForm('DialogForm')
temporaryHandlerDialog
  .getApi(async (_, row) => {
    const { subAccountId, accountName, startDate, endDate, flowerName } = row
    return { subAccountId, accountName, startDate, endDate: endDate || '2188-01-01', flowerName }
  })
  .postApi(addTemporaryCustomer)
  .title('添加临时处理人')
  .useGroup()
    .useItem('accountName').label('原子账号').setProp('disabled', true).end()
    .useDatePicker('startDate').label('开始日期').required().end()
    .useDatePicker('endDate').label('截止日期').required().end()
    .useItem('flowerName').label('原客服').setProp('disabled', true).end()
    .useCustomItem(ExNameItem, 'companyNo').label('临时人员公司').required().end()
    .useCustomItem(UserIdItem, 'userId').label('临时人员').required().end()

export { addAccountDialog, dispatchAccountDialog, dispatchCompanyDialog, dispatchAccountAllDialog, dispatchEditDialog, dispatchLogDialog, dispatchEditWithCompanyDialog, temporaryHandlerDialog }
